package com.gin.no_gay_alliance.database.entity;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

import com.gin.common.utils.TimeAgo;
import com.gin.nga.enums.AccountStatus;
import com.gin.nga.response.field.Money;
import com.gin.nga.response.field.user.BaseUserInfo;
import com.gin.nga.response.field.user.UserInfoNuke;

import java.util.Locale;

/**
 * 用户信息, 用户缓存
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/11 14:45
 **/
@Entity
public class NgaUserInfo {
    /**
     * 用户id
     */
    @PrimaryKey
    public long userId;
    /**
     * 用户名
     */
    public String username;
    /**
     * 账号状态(用于他人用户中心显示账号状态)
     */
    public AccountStatus accountStatus;
    /**
     * 状态最后更新时间
     */
    public long accountStatusTimestamp;
    /**
     * IP属地(用于在主题内显示用户IP属地)
     */
    public String ipLocation;
    /**
     * IP最后更新时间
     */
    public long ipLocationTimestamp;
    /**
     * 货币
     */
    public Money money;
    /**
     * 货币最后更新时间
     */
    public long moneyTimestamp;


    public NgaUserInfo(UserInfoNuke data) {
        this((BaseUserInfo) data);
        long now = System.currentTimeMillis() / 1000;
        ipLocation = data.getIpLocation();
        ipLocationTimestamp = now;
    }

    public NgaUserInfo(BaseUserInfo data) {
        long now = System.currentTimeMillis() / 1000;

        userId = data.getUserId();
        username = data.getUsername();
        if (data.getAccountStatus() != null) {
            accountStatus = data.getAccountStatus();
            accountStatusTimestamp = now;
        }
        if (data.getMoney() != null && data.getMoney().getRaw() != 0) {
            money = data.getMoney();
            moneyTimestamp = now;
        }
    }

    public NgaUserInfo() {
    }

    @NonNull
    @Override
    public String toString() {
        return String.format(Locale.CHINA, "%s (%d) 状态: %s(%s) IP:%s(%s)",
                username, userId,
                accountStatus, TimeAgo.ago(accountStatusTimestamp),
                ipLocation, TimeAgo.ago(ipLocationTimestamp)
        );
    }
}
